/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.tags;

import ca.trader.logger.TraderLogger;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Ahsan
 */
public class PrintPageNumbers extends SimpleTagSupport
{

	 private static final String _className = PrintPageNumbers.class.getName();
	 private JspWriter writer;
	 private Integer currentPageNumber;
	 private Integer totalPageNumbers;
	 private static final int NUM_PAGE_ENTERIES = 8;
	 private static final int END_POINTS = 2;
	 private String redirectPattern;

	 /**
	  * Called by the container to invoke this tag. The implementation of this
	  * method is provided by the tag library developer, and handles all tag
	  * processing, body iteration, etc.
	  */
	 @Override
	 public void doTag() throws JspException
	 {
		  final String _methodName = "doTag";
		  writer = getJspContext().getOut();

		  try
		  {
			   TraderLogger.logInfo(_className, _methodName, "Starting printing page numbers");
			   int interval[] = getInterval();

			   TraderLogger.logInfo(_className, _methodName, String.format("Interval:  [%d, %d] currentPage = %d, totalPages = %d",
					 interval[0], interval[1], currentPageNumber + 1, totalPageNumbers));

			   writer.print("<table class=\"pageNumbersTable\"><tbody><tr><td>Page: ");

			   if (currentPageNumber > 0)
			   {
				    printPageNumber("Prev", currentPageNumber - 1);
			   }

			   if (interval[0] > 0)
			   {
				    int end = Math.min(END_POINTS, interval[0]);

				    for (int i = 0; i < end; i++)
				    {
						printPageNumber(i);

				    }

				    printPageNumber("...");
			   }

			   for (int i = interval[0]; i < interval[1]; i++)
			   {
				    printPageNumber(i);
			   }

			   if (interval[1] < totalPageNumbers)
			   {
				    printPageNumber("...");


				    int begin = Math.max(totalPageNumbers - END_POINTS, interval[1]);
				    for (int i = begin; i < totalPageNumbers; i++)
				    {
						printPageNumber(i);
				    }

			   }
			   if (currentPageNumber < totalPageNumbers - 1)
			   {
				    printPageNumber("Next", currentPageNumber + 1);
			   }


			   writer.println("</td></tr></tbody></table>");
		  }
		  catch (java.io.IOException ex)
		  {
			   TraderLogger.logSevere(_className, _methodName, "Unknown exception", ex);
		  }
	 }

	 public void setCurrentPageNumber(Integer currentPageNumber)
	 {
		  TraderLogger.logInfo(_className, "setCurrentPageNumber", "Setting current page number: " + currentPageNumber);
		  this.currentPageNumber = currentPageNumber - 1;
	 }

	 public void setTotalPageNumbers(Integer totalPageNumbers)
	 {
		  TraderLogger.logInfo(_className, "setTotalPageNumbers", "Setting total page numbers: " + totalPageNumbers);
		  this.totalPageNumbers = totalPageNumbers;
	 }

	 public void setRedirectOutputUrlPattern(String redirectPattern)
	 {
		  TraderLogger.logInfo(_className, "setRedirectOutputUrlPattern", "Setting re-direct pattern: " + redirectPattern);
		  this.redirectPattern = redirectPattern;
	 }

	 public int[] getInterval()
	 {
		  int displayEnteriesHalf = NUM_PAGE_ENTERIES / 2;
		  int upperLimit = totalPageNumbers - displayEnteriesHalf;

		  int start = currentPageNumber > displayEnteriesHalf ? Math.max(Math.min(currentPageNumber - displayEnteriesHalf, upperLimit), 0) : 0;
		  int end = currentPageNumber > displayEnteriesHalf ? Math.min(currentPageNumber + displayEnteriesHalf, totalPageNumbers) : Math.min(NUM_PAGE_ENTERIES, totalPageNumbers);

		  return new int[]
				{
					 start, end
				};
	 }
	 
	 private void printPageNumber(String link) throws IOException
	 {
		  writer.print(String.format("<div>%s</div>", link));
	 }

	 private void printPageNumber(String link, int pageNumber) throws IOException
	 {
		  writer.print(String.format("<div><a href=\"" + redirectPattern + "\">%s</a></div>", pageNumber + 1, link));
	 }
	 

	 private void printPageNumber(int number) throws IOException
	 {

		  number = number < 0 ? 0 : (number < totalPageNumbers ? number : totalPageNumbers - 1);
		  if (number == currentPageNumber)
		  {
			   writer.print("<div>" + (number + 1) + "</div>");
		  }
		  else
		  {
			   writer.println(String.format("<div><a href=\"" + redirectPattern + "\">%d</a></div>", number + 1, number + 1));
		  }
	 }

}
